﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TimKen.DataAccess;
 
namespace TimKen.BussLogic
{

    /// <summary>
    /// 类Industry。
    /// </summary>
    public class Industry
    {
        public Industry()
        { }
        #region Model
        private int _industryid;
        private int? _parentid;
        private string _namecn;
        private string _descriptioncn;
        private string _nameen;
        private string _descriptionen;
        private string _image;
        /// <summary>
        /// 
        /// </summary>
        public int IndustryID
        {
            set { _industryid = value; }
            get { return _industryid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? ParentID
        {
            set { _parentid = value; }
            get { return _parentid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string NameCn
        {
            set { _namecn = value; }
            get { return _namecn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string DescriptionCn
        {
            set { _descriptioncn = value; }
            get { return _descriptioncn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string NameEn
        {
            set { _nameen = value; }
            get { return _nameen; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string DescriptionEn
        {
            set { _descriptionen = value; }
            get { return _descriptionen; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Image
        {
            set { _image = value; }
            get { return _image; }
        }
        #endregion Model


        #region  成员方法

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Industry(int IndustryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select IndustryID,ParentID,NameCn,DescriptionCn,NameEn,DescriptionEn,Image ");
            strSql.Append(" FROM Industry ");
            strSql.Append(" where IndustryID=@IndustryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@IndustryID", SqlDbType.Int,4)};
            parameters[0].Value = IndustryID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["IndustryID"].ToString() != "")
                {
                    IndustryID = int.Parse(ds.Tables[0].Rows[0]["IndustryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                NameCn = ds.Tables[0].Rows[0]["NameCn"].ToString();
                DescriptionCn = ds.Tables[0].Rows[0]["DescriptionCn"].ToString();
                NameEn = ds.Tables[0].Rows[0]["NameEn"].ToString();
                DescriptionEn = ds.Tables[0].Rows[0]["DescriptionEn"].ToString();
                Image = ds.Tables[0].Rows[0]["Image"].ToString();
            }
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int IndustryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Industry");
            strSql.Append(" where IndustryID=@IndustryID ");

            SqlParameter[] parameters = {
					new SqlParameter("@IndustryID", SqlDbType.Int,4)};
            parameters[0].Value = IndustryID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Industry(");
            strSql.Append("ParentID,NameCn,DescriptionCn,NameEn,DescriptionEn,Image)");
            strSql.Append(" values (");
            strSql.Append("@ParentID,@NameCn,@DescriptionCn,@NameEn,@DescriptionEn,@Image)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@NameCn", SqlDbType.NVarChar,100),
					new SqlParameter("@DescriptionCn", SqlDbType.NVarChar,3000),
					new SqlParameter("@NameEn", SqlDbType.NVarChar,50),
					new SqlParameter("@DescriptionEn", SqlDbType.NVarChar,3000),
					new SqlParameter("@Image", SqlDbType.VarChar,64)};
            parameters[0].Value = ParentID;
            parameters[1].Value = NameCn;
            parameters[2].Value = DescriptionCn;
            parameters[3].Value = NameEn;
            parameters[4].Value = DescriptionEn;
            parameters[5].Value = Image;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Industry set ");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("NameCn=@NameCn,");
            strSql.Append("DescriptionCn=@DescriptionCn,");
            strSql.Append("NameEn=@NameEn,");
            strSql.Append("DescriptionEn=@DescriptionEn,");
            strSql.Append("Image=@Image");
            strSql.Append(" where IndustryID=@IndustryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@IndustryID", SqlDbType.Int,4),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@NameCn", SqlDbType.NVarChar,100),
					new SqlParameter("@DescriptionCn", SqlDbType.NVarChar,3000),
					new SqlParameter("@NameEn", SqlDbType.NVarChar,50),
					new SqlParameter("@DescriptionEn", SqlDbType.NVarChar,3000),
					new SqlParameter("@Image", SqlDbType.VarChar,64)};
            parameters[0].Value = IndustryID;
            parameters[1].Value = ParentID;
            parameters[2].Value = NameCn;
            parameters[3].Value = DescriptionCn;
            parameters[4].Value = NameEn;
            parameters[5].Value = DescriptionEn;
            parameters[6].Value = Image;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int IndustryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Industry ");
            strSql.Append(" where IndustryID=@IndustryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@IndustryID", SqlDbType.Int,4)};
            parameters[0].Value = IndustryID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int IndustryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 IndustryID,ParentID,NameCn,DescriptionCn,NameEn,DescriptionEn,Image ");
            strSql.Append(" FROM Industry ");
            strSql.Append(" where IndustryID=@IndustryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@IndustryID", SqlDbType.Int,4)};
            parameters[0].Value = IndustryID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["IndustryID"].ToString() != "")
                {
                     IndustryID = int.Parse(ds.Tables[0].Rows[0]["IndustryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                     ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                 NameCn = ds.Tables[0].Rows[0]["NameCn"].ToString();
                 DescriptionCn = ds.Tables[0].Rows[0]["DescriptionCn"].ToString();
                 NameEn = ds.Tables[0].Rows[0]["NameEn"].ToString();
                 DescriptionEn = ds.Tables[0].Rows[0]["DescriptionEn"].ToString();
                 Image = ds.Tables[0].Rows[0]["Image"].ToString();
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Industry ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}

